package com.guoshengdeu.door.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guoshengdeu.door.entity.Article;
import com.guoshengdeu.door.service.IArticleService;
import com.guoshengdeu.door.util.RespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yfly
 * @since 2021-06-01
 */
@Api("文章接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    //查询article
    @ApiOperation("查询所有")
    @RequestMapping("list")
    public List<Article> queryArticle(){
        return articleService.list();
    }

    @ApiOperation("根据id查询")
    @RequestMapping("querybyid")
    public RespResult queryArctleById(int id){
        Article byId = articleService.getById(id);
        return RespResult.success(byId);
    }

    //添加更新
    @ApiOperation("添加更新")
    @RequestMapping("addarticle")
    public RespResult addArticle(@RequestBody Article article){
        boolean saveOrUpdate = articleService.saveOrUpdate(article);
        if (saveOrUpdate){
            return RespResult.success();
        }
        return RespResult.error("添加或更新失败");
    }

    //删除article
    @ApiOperation("删除")
    @DeleteMapping("delearticle")
    public RespResult deleArticle(int id){
        boolean remove = articleService.removeById(id);
        if (remove){
            return RespResult.success();
        }
        return RespResult.error("删除失败");
    }

    //分页
    @ApiOperation("分页")
    @GetMapping("page")
    public RespResult page(Page<Article> page){
        Page<Article> pages = articleService.page(page);
        return RespResult.success(pages);
    }
}
